<?php
/**
 * @version		$Id: search.php 19343 2010-11-03 18:12:02Z ian $
 * @package		Joomla
 * @subpackage	Search
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

/**
 * @package		Joomla
 * @subpackage	Search
 */
class Mwc_EmployeeOrgModelMwc_EmployeeOrg extends JModel{

	var $lists = '';

	/**
	 * Overridden constructor
	 * @access	protected
	 */
	function __construct(){
		parent::__construct();
	}

	function reset(){
		$db =& JFactory::getDBO();
		$db->setQuery( 'DELETE FROM #__mwc_employee' );
		$db->query();
	}

	function getItems( ){
		global $mainframe, $option;
		$db	=& JFactory::getDBO();        

        
		$filter_order		= $mainframe->getUserStateFromRequest( 'com_mwc_employee.org.filter_order',		'filter_order',		'',     'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( 'com_mwc_employee.org.filter_order_Dir',	'filter_order_Dir',	'',		'word' );
		$limit				= $mainframe->getUserStateFromRequest( 'global.list.limit',				        'limit',			$mainframe->getCfg('list_limit'), 'int' );
        $filter_state       = $mainframe->getUserStateFromRequest( 'com_mwc_employee.org.filter_state',     'filter_state',        '',            'word' );
		$limitstart			= $mainframe->getUserStateFromRequest( 'com_mwc_employee.org.limitstart',		'limitstart',		0,		'int' );
		$search				= $mainframe->getUserStateFromRequest( 'com_mwc_employee.org.search',			'search',			'',		'string' );
        $filter_dep         = $mainframe->getUserStateFromRequest( 'com_mwc_employee.org.filter_dep',       'filter_dep',        '',            'int' );
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);

		if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC'))) {
			$filter_order_Dir = '';
		}

		// table ordering
		if ( strtoupper($filter_order_Dir) == 'ASC' ) {
			$this->lists['order_Dir'] = 'ASC';
		} else {
			$this->lists['order_Dir'] = 'DESC';
		}
		$this->lists['order'] = $filter_order;

		// search filter
		$this->lists['search']= $search;
        
        // state filter
        $this->lists['state']    = JHTML::_('grid.state',  $filter_state ); 
        
        // department filter
        $departments = $this->_getDepartments();
        $this->lists['departments_filter'] = JHTML::_('select.genericlist',  $departments, 'filter_dep', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'id', 'name', $filter_dep);
           
		$where = array();
        if ($filter_state != "") {
            $filter_state = $filter_state == 'P' ? 1 : 0;
            $where[] = 'e.published = ' . (int) $filter_state;
        }
        if ($filter_dep > 0) {
            $where[] = 'e.dep_id = ' . (int) $filter_dep;
        }
        if ( $search ) {
            $where[] = 'e.name LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
        }
		$where = ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

        if($filter_order)
		    $orderby 	= ' ORDER BY '. $filter_order .' '. $filter_order_Dir;

		// get the total number of records
		$query = 'SELECT COUNT(*)' .
		    ' FROM #__mwc_employee e' . 
		    $where;
		$db->setQuery( $query );     
		$total = $db->loadResult();

		jimport( 'joomla.html.pagination' );
		$pageNav = new JPagination( $total, $limitstart, $limit );

		$query = ' SELECT e.*, d.name AS department ' .
		    ' FROM #__mwc_employee e INNER JOIN #__mwc_employee_dep d ON e.dep_id = d.id' .
		    $where .
		    $orderby;             
		$db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
		$rows = $db->loadObjectList();

		return $rows;
	}
    
    function getItem(){
        global $mainframe, $option;
        
        $db     =& JFactory::getDBO();   
        $cid    = JRequest::getVar('cid', array(0), '', 'array');
        JArrayHelper::toInteger($cid, array(0));
        
        $row =& JTable::getInstance('mwc_employee', 'Table');         
        // load the row from the db table
        if($cid[0] > 0)
            $row->load( $cid[0] );
    
        $this->lists['published'] = JHTML::_('select.booleanlist',  'published', '', $row->published ); 
        
        $departments = $this->_getDepartments();
        $this->lists['departments'] = JHTML::_('select.genericlist',  $departments, 'dep_id', 'class="inputbox" size="1" ', 'id', 'name', intval($row->dep_id));
                    
        return $row;
    }
    
    function _getDepartments(){
        $db     =& JFactory::getDBO();
        
        $query = ' SELECT * FROM #__mwc_employee_dep';             
        $db->setQuery( $query );
        $deps = $db->loadObjectList();
        $departments[] = JHTML::_('select.option', '0', '- '.JText::_('Select Department').' -', 'id', 'name');        
        $departments = array_merge($departments, $deps);
        
        return $departments;
    }
}
